package com.agile.leetcode.canculateObjectSize;

import com.agile.leetcode.canculateObjectSize.unsafe.UnsafeUtils;
import org.openjdk.jol.info.ClassLayout;
import sun.misc.Unsafe;

/**
 * @Author ChenZhangKun
 * @Date 2020/12/25 13:25
 */
public class JolCore {
    public static void main(String[] args) throws NoSuchFieldException {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 10000; i++) {
            stringBuilder.append("czk");
        }
        Person person = new Person(stringBuilder.toString(), 25, 175);
        System.out.println(ClassLayout.parseInstance(person).toPrintable());
        // 拿到age偏移量
        Unsafe unsafe = UnsafeUtils.getUnsafe();
        System.out.println(unsafe.objectFieldOffset(person.getClass().getDeclaredField("name")));
        System.out.println(person.hashCode());
        // 数组长度的验证
        // 对象头多了数组的长度，在32位和64位虚拟机中的长度都是4个字节
        // 对象头是实例数据之间有数据填充，如果对象头的大小不是8的整数倍，则会进行数据填充进行对齐
        Person[] people = new Person[]{new Person("czj", 28, 180)};
        System.out.println(ClassLayout.parseInstance(people).toPrintable());
    }
}
